package com.market.config;

import com.market.utils.StringUtil;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 保证跨域场景下的session一致问题
 *
 * @author tan
 * @date 2022年12月30日 17:55
 */
public class MarketSessionManager extends DefaultWebSessionManager {

    private static final String MARKET_ADMIN_TOKEN = "X-CskaoyanMarket-admin-Token";
    private static final String MARKET_WX_TOKEN = "X-CskaoyanMarket-Token";

    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String sessionId = httpServletRequest.getHeader(MARKET_ADMIN_TOKEN);
        /*if (sessionId != null & !"".equals(sessionId)) {
            return sessionId;
        }*/
        if (!StringUtil.isEmpty(sessionId)) {
            return sessionId;
        }
        String sessionId2 = httpServletRequest.getHeader(MARKET_WX_TOKEN);
        if (!StringUtil.isEmpty(sessionId2)) {
            return sessionId2;
        }
        return super.getSessionId(request, response);
    }
}
